package com.xx.service.Impl;

import com.xx.dao.auction.AuctionDao;
import com.xx.dao.auctionuser.UserDao;
import com.xx.entity.Auction;
import com.xx.service.AuctionService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author 许欣
 * 时间：2022/4/13 9:56
 * 描述：
 */
public class AuctionServiceImpl implements AuctionService {
    @Override
    public int update(Auction auction) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AuctionDao dao=sqlSession.getMapper(AuctionDao.class);
        int ret=dao.update(auction);
        sqlSession.commit();
        sqlSession.rollback();
        sqlSession.close();
        return ret;
    }

    @Override
    public int delete(Integer auctionId) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AuctionDao dao=sqlSession.getMapper(AuctionDao.class);
        int ret=dao.delete(auctionId);
        sqlSession.commit();
        sqlSession.rollback();
        sqlSession.close();
        return ret;
    }

    @Override
    public List<Auction> getAuction() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AuctionDao dao=sqlSession.getMapper(AuctionDao.class);
        List<Auction>list=dao.getAll();
        return list;
    }

    @Override
    public List<Auction> getAll() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AuctionDao dao=sqlSession.getMapper(AuctionDao.class);
        List<Auction> ret=dao.getAll();
        return ret;
    }

    @Override
    public Auction getId(Integer auctionId) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AuctionDao dao=sqlSession.getMapper(AuctionDao.class);
        Auction auction=dao.getId(Integer.valueOf(auctionId));
        return auction;
    }

    @Override
    public List<Auction> getLike(String auctionName) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AuctionDao dao=sqlSession.getMapper(AuctionDao.class);
        //AuctionRecordService dao=new AuctionRecordServiceImpl();
        List<Auction> list=dao.getLike(auctionName);
        return list;
    }

    @Override
    public int save(Auction auction) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AuctionDao dao=sqlSession.getMapper(AuctionDao.class);
        int ret=dao.save(auction);
        sqlSession.commit();
        sqlSession.rollback();
        sqlSession.close();
        return ret;
    }
}
